/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package program.properties;

import java.io.*;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import program.beans.Property;

/**
 *
 * Responsible: Johannes
 */
public class PropertyManager {

    private ArrayList<Property> properties = new ArrayList<>();
    private static PropertyManager instance;
    private final File propertyFile = new File("properties.ser");

    private PropertyManager() {
        try {
            this.readAllProperties();
        } catch (Exception ex) {
                       JOptionPane.showMessageDialog(null, ex);

        }
    }

    public static PropertyManager getInstance() {
        if (instance == null) {
            instance = new PropertyManager();
        }

        return instance;
    }

    private void readAllProperties() throws Exception {
        properties.clear();

        FileInputStream fis = new FileInputStream(propertyFile);
        ObjectInputStream ois = new ObjectInputStream(fis);

        try {
            while (true) {
                Property p = (Property) ois.readObject();
                properties.add(p);
                
            }
        } catch (EOFException e) {
        }
    }

    private void writeAllProperties() throws Exception {
        FileOutputStream fos = new FileOutputStream(propertyFile);
        ObjectOutputStream oos = new ObjectOutputStream(fos);

        for (Property p : properties) {
            oos.writeObject(p);
        }

        oos.close();
    }

    public void addProperty(String key, String value) throws Exception {
        for (Property p : properties) {
            if (p.getKey().equals(key)) {
                throw new Exception("Property ist bereits vorhanden!");
            }
        }

        properties.add(new Property(key, value));

        this.writeAllProperties();
    }
    
        public void remove(String key) throws Exception {
            Property p1 = null;
        for (Property p : properties) {
            if (p.getKey().equals(key)) {
                p1 = p;
                break;
            }
        }

        properties.remove(p1);

        this.writeAllProperties();
    }

    public String getValue(String key) throws Exception {
        for (Property p : properties) {
        
            if (p.getKey().equals(key)) {
                
                return p.getText();
            }
        }
        return null;
    }

    public void updateProperty(String key, String newValue) throws Exception {
        Property p = properties.get(properties.indexOf(new Property(key, "")));

        if (p != null) {
            p.setText(newValue);
            this.writeAllProperties();
        } else {
            throw new Exception("Property '"+key+"' kann nicht gefunden werden!");
        }
    }
    
    public void printProperties()
    {
        for(Property p : properties)
            System.out.println(p);
    }

    public static void main(String[] args) throws Exception {
        PropertyManager pm = getInstance();

        pm.remove("uid");
        pm.remove("uName");
        pm.printProperties();
        
     //   pm.updateProperty("uid", null);
    }
}
